/**
 * Copyright (C), 2020, 安徽贝慕信息科技有限公司
 * FileName: ServiceResourceControllerImpl
 * Author:   Allen
 * Date:     2020/8/1 14:33
 * Description: 服务资源处理业务实现
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.bim.marvel.rest.impl;

import com.bim.marvel.openapi.client.dto.ServiceResourceDTO;
import com.bim.marvel.openapi.client.query.ServiceResourceQuery;
import com.bim.marvel.openapi.core.entity.ServiceResourceDO;
import com.bim.marvel.openapi.core.service.impl.ServiceResourceService;
import com.bim.marvel.rest.ServiceResourceController;
import com.bim.marvel.common.core.constant.CodeEnum;
import com.bim.marvel.common.core.model.dto.SimpleVO;
import com.bim.marvel.common.util.SimpleConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * 〈服务资源处理业务实现〉
 *
 * @author Allen
 * @date 2020/8/1
 * @since 1.0.0
 */
@RestController
public class ServiceResourceControllerImpl implements ServiceResourceController {

    /**
     * 注入ServiceResourceService
     */
    @Autowired
    private ServiceResourceService serviceResourceService;

    @Override
    @CacheEvict(value = "getServiceResourceList", allEntries = true)
    public SimpleVO<Boolean> saveServiceResource(@Valid ServiceResourceQuery serviceResourceQuery) {
        SimpleVO<Boolean> simpleVO = new SimpleVO(CodeEnum.SUCCESS);
        boolean success = serviceResourceService.saveOrUpdateServiceResource(serviceResourceQuery);
        if (!success) {
            simpleVO = new SimpleVO(CodeEnum.INSERT_FAILED);
        }
        return simpleVO;
    }

    @Override
    @CacheEvict(value = "getServiceResourceList", allEntries = true)
    public SimpleVO<Boolean> updateServiceResource(@Valid ServiceResourceQuery serviceResourceQuery) {
        SimpleVO<Boolean> simpleVO = new SimpleVO(CodeEnum.SUCCESS);
        boolean success = serviceResourceService.saveOrUpdateServiceResource(serviceResourceQuery);
        if (!success) {
            simpleVO = new SimpleVO(CodeEnum.UPDATE_FAILED);
        }
        return simpleVO;
    }

    @Override
    @Cacheable(value = "getServiceResourceList")
    public SimpleVO<List<ServiceResourceDTO>> getServiceResourceList() {
        List<ServiceResourceDO> serviceResourceDOList = serviceResourceService.list();
        if (CollectionUtils.isEmpty(serviceResourceDOList)) {
            return new SimpleVO<>(CodeEnum.DATA_NOT_FOUND);
        }
        List<ServiceResourceDTO> serviceResourceDTOList = SimpleConverter.convert(serviceResourceDOList,
                ServiceResourceDTO.class);
        return new SimpleVO<>(serviceResourceDTOList);
    }
}
